<template>
    <div>
        <h1>人员列表</h1>
        <h3 style="color: red;">Count组件求和为：{{sum}}</h3>
        <input type="text" placeholder="请输入名字" v-model="name">
        <button @click="add()">添加</button>
        <ul>
            <li v-for="person in personList" :key="person.id">{{person.name}}</li>
        </ul>
    </div>
</template>

<script>
    import { nanoid } from 'nanoid';

    export default {
        name: 'Person',
        data() {
            return {
                name: ''
            }
        },
        methods: {
            add() {
                const personObj = {
                    id: nanoid(),
                    name: this.name,
                }
                console.log('@personObj', personObj);
                console.log("name:", this.name);
                if (this.name == null || this.name == '') {
                    alert('请输入name！');
                    return;
                }
                // 调用vuex，将该用户添加到store管理的state里面即可。
                this.$store.commit('ADD_PERSON', personObj);
                this.name = '';
            }
        },
        computed: {
            personList() {
                return this.$store.state.personList;
            },
            sum() {
                return this.$store.state.sum;
            }
        }
    }
</script>

<style>

</style>